/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.lang.*;
import java.applet.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;

public class Display extends JFrame
{
     Image image = null;
     MyCanvas panel;
     Graphics gr;
     String fileName;
     JScrollPane myScrollPane;
     int currentScale;


     public Display(String inName) throws Exception
     {
          this.setSize(500,1000);
          fileName = inName;
          panel = new MyCanvas(image, 500, 1000);
          Dimension dimension = new Dimension(500, 1000);
          panel.setPreferredSize(dimension);
          panel.setSize(dimension);
          myScrollPane = new JScrollPane(panel);
          this.getContentPane().add(myScrollPane);

          this.setBackground(Color.white);
          this.setVisible(true);
          this.addWindowListener(
               new WindowAdapter(){
                    public void windowClosing(WindowEvent evt){
                         System.exit(0);
                    }
               }
          );
          display(1, fileName);
     }


     public void display(int scale, String s) throws Exception
     {
          currentScale = scale;
          FileInputStream fis = new FileInputStream(s);
          byte[] readIn = new byte[1];
          boolean startPixel = false;
          int nextByte = 0;
          int newLineCount=0;
          int spaceCount = 0;
          LinkedList<Integer> widthCharArray = new LinkedList<Integer>();
          LinkedList<Integer> heightCharArray = new LinkedList<Integer>();
          while(!startPixel)
          {
               nextByte = fis.read(readIn);
               Byte firstB = readIn[0];
               int separatorValue = new Byte(Character.DIRECTIONALITY_PARAGRAPH_SEPARATOR).intValue();
               if(firstB.intValue() == separatorValue)
               {
                    newLineCount++;
               }
               if(newLineCount == 2)
               {
                    String st = new String(readIn);
                    Character firstC = st.charAt(0);
                    if(Character.isSpaceChar(firstC))
                    {
                         spaceCount++;
                    }else
                    {
                         if(firstB.intValue() != separatorValue)
                         {
                              if(spaceCount == 0)
                              {
                                   int temp = (int)(new Integer(st));
                                   widthCharArray.add(new Integer(new String(readIn)));
                              }
                              if(spaceCount == 1)
                              {
                                   int temp = (int)(new Integer(st));
                                   heightCharArray.add(new Integer(new String(readIn)));
                              }
                         }
                    }
               }
               if(newLineCount == 4)
               {
                    startPixel = true;
               }
          }
          int tempWidth = decodeIntArray(widthCharArray);
          int tempHeight = decodeIntArray(heightCharArray);

        int newWidth = tempWidth/scale;
        int newHeight = tempHeight/scale;

          Image tempImage = new java.awt.image.BufferedImage(newWidth, newHeight, java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
         Graphics2D tempgr = (Graphics2D)tempImage.getGraphics();
         Graphics2D g2 = (Graphics2D) tempgr;    //generate an instance of Graphics2D and set the background color to be white
         g2.setBackground(Color.white);
         g2.clearRect(0,0,newWidth, newHeight);
         if(scale < 10 )
         {
               readIn = new byte[tempWidth];
               int lineCount = 0;
               int index = 0;
               while(fis.read(readIn) != -1)
               {
                    while(index < readIn.length)
                    {
                         int newX = index/scale;
                         int newY = lineCount/scale;
                         if(index%scale == 0 && lineCount%scale == 0)
                         {
                              int tempInt = (int)(readIn[index]&0xff);
                              if(tempInt == 0)
                              {
                                   tempgr.setColor(Color.black);
                                   tempgr.drawLine(newX, newY, newX, newY);
                              }
                              if(tempInt == 180)
                              {
                                   tempgr.setColor(Color.red);
                                   tempgr.drawLine(newX, newY, newX, newY);
                              }
                         }
                         index++;
                    }
                    index = 0;
                    lineCount++;
               }
          }
          fis.close();
          Dimension dimension = new Dimension(newWidth, newHeight);
          panel.setPreferredSize(dimension);
          panel.setSize(newWidth, newHeight);
          panel.setImage(tempImage);
     }

    public int decodeIntArray(LinkedList<Integer> inputArray)    //compose an int value from an array with elements of one digit
    {
          int value = 0;
          int i = 0;
          while(i < inputArray.size())
          {
               value = (int) (inputArray.get(i)*Math.pow(10,inputArray.size()-i-1) + value);
               i++;
          }
          return value;
    }

     public class MyCanvas extends JLabel
     {
          Image image = null;
          int width;
          int height;

          public MyCanvas(Image im, int h, int w)
          {
               image = im;
               width = w;
               height = h;
          }

          public void setImage(Image im)
          {
               this.image = im;
               repaint();
          }

          public void reSize(int w, int h)
          {
               this.setSize(w, h);
          }

          public void paint(Graphics gr)
          {
               super.paint(gr);
               if(image != null)
               {
                    gr.drawImage(image, 0, 0, this);
               }
          }
     }

     public static void main(String args[]) throws Exception{
          new Display("C:\\u.jpg");
     }
}